home *** CD-ROM | disk | FTP | other *** search
/ Night Owl 6 / Night Owl's Shareware - PDSI-006 - Night Owl Corp (1990).iso / 039a / mawk10.zip / MAIN.C < prev    next >
C/C++ Source or Header  |  1991-10-05  |  4KB  |  205 lines

  1.  
  2. /********************************************
  3. main.c
  4. copyright 1991, Michael D. Brennan
  5.  
  6. This is a source file for mawk, an implementation of
  7. the AWK programming language.
  8.  
  9. Mawk is distributed without warranty under the terms of
  10. the GNU General Public License, version 2, 1991.
  11. ********************************************/
  12.  
  13. /* $Log:    main.c,v $
  14.  * Revision 3.4.1.1  91/09/14  17:23:39  brennan
  15.  * VERSION 1.0
  16.  * 
  17.  * Revision 3.4  91/08/13  06:51:43  brennan
  18.  * VERSION .9994
  19.  * 
  20.  * Revision 3.3  91/06/29  09:47:14  brennan
  21.  * Only track NR if needed
  22.  * 
  23.  * Revision 3.2  91/06/28  04:16:58  brennan
  24.  * VERSION 0.999
  25.  * 
  26.  * Revision 3.1  91/06/08  06:14:36  brennan
  27.  * VERSION 0.995
  28.  * 
  29.  * Revision 2.6  91/06/08  06:00:18  brennan
  30.  * changed how eof is marked on main_fin
  31.  * 
  32.  * Revision 2.5  91/06/05  07:20:22  brennan
  33.  * better error messages when regular expression compiles fail
  34.  * 
  35.  * Revision 2.4  91/05/30  11:14:38  brennan
  36.  * added static 
  37.  * 
  38.  * Revision 2.3  91/05/16  12:19:56  brennan
  39.  * cleanup of machine dependencies
  40.  * 
  41.  * Revision 2.2  91/04/22  08:08:58  brennan
  42.  * cannot close(3) or close(4) because of bug in TurboC++ 1.0
  43.  * 
  44.  * Revision 2.1  91/04/08  08:23:27  brennan
  45.  * VERSION 0.97
  46.  * 
  47. */
  48.  
  49.  
  50.  
  51. /*  main.c  */
  52.  
  53. #include "mawk.h"
  54. #include "code.h"
  55. #include "init.h"
  56. #include "fin.h"
  57. #include "bi_vars.h"
  58. #include "field.h"
  59. #include "files.h"
  60. #include <stdio.h>
  61.  
  62. #if  MSDOS 
  63. void  reargv(int *, char ***) ;
  64. #endif
  65.  
  66.  
  67. static void  PROTO( execution, (void) ) ;
  68. static void  PROTO( main_loop, (void) ) ;
  69.  
  70. extern int program_fd ;
  71. char *progname ;
  72. short mawk_state ; /* 0 is compiling */
  73. short NR_reference ;
  74.  
  75. jmp_buf   exit_jump, next_jump ;
  76. int  exit_code ;
  77.  
  78.  
  79. main(argc , argv )
  80.   int argc ; char **argv ;
  81.  
  82. #if   MSDOS
  83.   progname = "mawk" ;
  84. #if      HAVE_REARGV
  85.   reargv(&argc, &argv) ;
  86. #endif
  87. #else    /* MSDOS */
  88. #ifdef THINK_C
  89.   progname = "MacMAWK";
  90. #else    /* THINK_C */
  91.   { char *strrchr() ;
  92.     char *p = strrchr(argv[0], '/') ;
  93.     progname = p ? p+1 : argv[0] ; }
  94. #endif
  95. #endif
  96.  
  97.   initialize(argc, argv) ;
  98.  
  99.   if ( parse() || compile_error_count )  mawk_exit(1) ;
  100.  
  101.   compile_cleanup() ;
  102.   mawk_state = EXECUTION ;
  103.   execution() ;
  104.  
  105.   mawk_exit( exit_code ) ;
  106.   return 0 ;
  107. }
  108.  
  109.  
  110. static  void  execution()
  111. {
  112.  
  113.   if ( setjmp(exit_jump) )
  114.   { if ( begin_start ) zfree(begin_start, begin_size) ;
  115.     goto the_exit ;
  116.   }
  117.  
  118.   if ( begin_start )
  119.   { (void) execute(begin_start, eval_stack-1, 0) ;
  120.     zfree( begin_start , begin_size ) ;
  121.     begin_start = (INST *) 0 ;
  122.   }
  123.  
  124.   if ( main_start || end_start )  main_loop() ;
  125.  
  126. the_exit:
  127.  
  128.   if ( setjmp(exit_jump) )  mawk_exit(exit_code) ;
  129.  
  130.   if ( main_start )  zfree(main_start, main_size) ;
  131.   if ( end_start ) (void) execute(end_start, eval_stack-1, 0) ;
  132. }
  133.  
  134.  
  135. static void  main_loop()
  136. { register char *p ;
  137.   unsigned len ;
  138.  
  139.   /* the main file stream might already be open by a call of
  140.      getline in the BEGIN block */
  141.  
  142.   if ( ! main_fin )  open_main()  ;
  143.  
  144.   if ( main_start )
  145.   {
  146.  
  147.      if ( NR_reference )
  148.      {
  149.          (void)  setjmp(next_jump) ;
  150.  
  151.      while ( p = FINgets( main_fin, &len ) )
  152.      { 
  153.        if ( TEST2(bi_vars + NR) != TWO_DOUBLES )
  154.             cast2_to_d(bi_vars + NR) ;
  155.  
  156.        bi_vars[NR].dval += 1.0 ;
  157.        bi_vars[FNR].dval += 1.0 ;
  158.  
  159.        set_field0(p, len) ;
  160.        (void) execute( main_start, eval_stack-1, 0) ;
  161.      }
  162.      }
  163.      else /* don't worry about NR */
  164.      {
  165.          (void)  setjmp(next_jump) ;
  166.  
  167.      while ( p = FINgets( main_fin, &len ) )
  168.      { 
  169.        set_field0(p, len) ;
  170.        (void) execute( main_start, eval_stack-1, 0) ;
  171.      }
  172.      }
  173.   }
  174.   else  /* eat main and set correct state */
  175.   { long nr ;
  176.     char *f0 ;
  177.     unsigned f0_len ;
  178.  
  179.     if ( TEST2(bi_vars+NR) != TWO_DOUBLES ) cast2_to_d(bi_vars+NR) ;
  180.     nr = (long) bi_vars[NR].dval ;
  181.     f0 = (char *) 0 ;
  182.  
  183.     while ( p = FINgets(main_fin, &len) )
  184.     {
  185.       f0 = p ; f0_len = len ;
  186.       nr++ ; bi_vars[FNR].dval += 1.0 ;
  187.     }
  188.     bi_vars[NR].dval = (double) nr ;
  189.     set_field0(f0, f0_len) ;
  190.   }
  191. }
  192.  
  193.  
  194. void  mawk_exit(x)
  195.   int x ;
  196. {
  197. #if  !  MSDOS
  198. #ifndef THINK_C
  199.   close_out_pipes() ;  /* no effect, if no out pipes */
  200. #endif
  201. #endif
  202.   exit(x) ;
  203. }
  204.